package com.xjh.t.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.domain.ClassLevel;
import com.xjh.t.domain.EnglishWord;
import com.xjh.t.domain.Lesson;
import com.xjh.t.domain.Sentence;
import com.xjh.t.service.IClassLevelService;
import com.xjh.t.service.IEnglishWordService;
import com.xjh.t.service.ISentenceService;
import com.xjh.t.vo.ClassLevelVo;
import com.xjh.t.vo.EnglishWordVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author satela
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/classlevel")
public class ClassLevelController {

    @Autowired
    private IClassLevelService classLevelService;


    @RequestMapping("/addClasslevel")
    public ResultObj addClasslevel(ClassLevel classLevel) {

        QueryWrapper<ClassLevel> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("level_type",classLevel.getLevelType());
        queryWrapper.eq("order_num",classLevel.getOrderNum());

        List<ClassLevel> list = this.classLevelService.list(queryWrapper);

        if(list.size() > 0)
        {
            return ResultObj.ADD_ERROR;
        }

        this.classLevelService.save(classLevel);

        return ResultObj.ADD_SUCESS;

    }


    @RequestMapping("/loadAllClass")
    public DataGridView loadAllWord(ClassLevelVo classLevelVo) {

        IPage<ClassLevel> page=new Page<>(classLevelVo.getPage(), classLevelVo.getLimit());
        QueryWrapper<ClassLevel> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(StringUtils.isNotBlank(classLevelVo.getLevelName()), "level_name",classLevelVo.getLevelName());
        queryWrapper.eq(classLevelVo.getLevelType() != null && classLevelVo.getLevelType() < 3, "level_type",classLevelVo.getLevelType());


        this.classLevelService.page(page, queryWrapper);



        return new DataGridView(page.getTotal(),page.getRecords());

    }

    @RequestMapping("/updateClasslevel")
    public ResultObj updateWord(ClassLevel classLevel) {

        this.classLevelService.updateById(classLevel);

        return ResultObj.UPDATE_SUCESS;

    }

    @RequestMapping("/loadAllClassList")
    public List<ClassLevel> loadAllClassList() {

        return this.classLevelService.list();

    }

    @RequestMapping("/getClasses")
    public Object getClasses(Integer type) {

        QueryWrapper<ClassLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level_type",type);

        JSONObject json = new JSONObject();
        json.put("code",200);
        json.put("classlist",this.classLevelService.list(queryWrapper));
        return json;

    }



}

